package com.pks.insurance.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.pks.insurance.domain.User;
import com.pks.insurance.helper.UserLevels;
import com.pks.insurance.service.UserManager;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = Logger.getLogger(LoginServlet.class);
	private UserManager userManager;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LOGGER.debug("begin");
		String ssn = request.getParameter("ssn");
		String passwd = request.getParameter("passwd");
		LOGGER.info("ssn: " + ssn + " passwd: " + passwd);
		User user = new User();
		user.setSsn(ssn);
		user.setPasswd(passwd);
		LOGGER.info("loginDto:" + user);
		ApplicationContext context = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		userManager = (UserManager) context.getBean("userManager");
		int authResult = userManager.authenticateUser(user);
		LOGGER.info("authResult: " + authResult);

		// For Normal Valid User
		if (authResult == UserLevels.VALID_USER) {
			HttpSession session = request.getSession(false);
			session.setAttribute("ssn", ssn);
			try {
				response.sendRedirect("home.jsp");
			} catch (IOException e) {
				LOGGER.error("Problem in redirection to home.jsp", e);
			}

		} else if (authResult == UserLevels.VALID_ADMIN) {
			// For Admin user
			HttpSession session = request.getSession(false);
			session.setAttribute("ssn", ssn);

			// Retrive list of all the users
			request.setAttribute("customers", userManager.getUsers());
			request.getRequestDispatcher("adminManage.jsp").forward(request,
					response);
			return;
		} else {
			// Display Error Message to the user for incorrect login credentials
			request.setAttribute("error",
					"The username or password you entered is incorrect.");
			request.getRequestDispatcher("/").forward(request, response);
		}
		LOGGER.debug("end");
	}

	/*
	 * Perform the Logout Action of the User
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String task = request.getParameter("task");
		LOGGER.info("task: " + task);
		if ("logout".equals(task)) {
			HttpSession session = request.getSession();
			session.invalidate();
			response.sendRedirect("");
		}
	}

}
